<?php
/*	
	结算
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'结算列表'),
	array('op'=>'add','title'=>'创建结算单'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('balance',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$merch=pdo_get('jy_merch_user',array('id'=>$item['merchid']),array('id','merchname','logo'));
			$item['member']=array(
				'nickname'=>$merch['merchname'],
				'avatar'=>$merch['logo'],	
			);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array(
			'tip'=>'[温馨提示]：考虑PC后台创建结算的麻烦性，所有结算订单的创建，直接由代理商客户手机端自由申请结算！',
			'data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'member','title'=>'商户','type'=>'member'),
			array('name'=>'balancesn','title'=>'结算单号','type'=>'string'),
			array('name'=>'commission_num','title'=>'佣金份数','type'=>'string'),
			array('name'=>'money','title'=>'佣金金额','type'=>'string'),
			array('name'=>'balance_pay','title'=>'手续费','type'=>'string'),
			array('name'=>'final_money','title'=>'打款金额','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'check_time','title'=>'审核时间','type'=>'time'),
			array('name'=>'pay_time','title'=>'打款时间','type'=>'time'),
			array('name'=>'status','title'=>'结算单状态','type'=>'radio','options'=>'balance_status'),
			array('name'=>'pay_type','title'=>'打款方式','type'=>'radio','options'=>'pay_types'),
		),'action'=>array(
			array('type'=>'modal','op'=>'check','title'=>'重新提交','show'=>array('status'=>2)),
			array('type'=>'modal','op'=>'remark','title'=>'查看原因','show'=>array('status'=>2)),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'balance_status'),
		),
	);
	$return['htmls']=$htmls;
	$return['balance_status']=$this->balance_status;
	$return['pay_types']=$this->pay_types;
	exi($return);
}
if($_RQ['op']=='remark'){
	$item=$this->get('balance',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定结算单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'查看原因',
		'look'=>array(
			array('type'=>'string','name'=>'remark','title'=>'审核结果','read'=>true),
		),
	);
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$balance_id=intval($post['id']);
		$this->update('balance',array('status'=>0,'remark'=>$post['remark']),array('id'=>intval($balance_id)));
		exi('提交成功！','','list');
	}
	$item=$this->get('balance',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定结算单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核结算单',
		'edit'=>array(
			array('name'=>'remark','title'=>'申请描述','type'=>'text'),
		),
	);
	exi($return);
}
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$balance_id=intval($post['id']);
		$this->model('commission')->create_balance($merchid,$post);
		exi('操作成功！','','list');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'status'=>1);
	$item['commission_1']=$this->sum('commission',$params,'money');
	$dhsettimg=$this->plugin_setting('dhsettimg');
	if($dhsettimg['balance_day']){
		//结算天数
		$time=TIMESTAMP-intval($dhsettimg['balance_day'])*86400;
		$params['complete_time <']=$time;
	}
	$item['commission_1_1']=$this->sum('commission',$params,'money');
	$item['service_money']=intval($dhsettimg['balance_per']*$item['commission_1_1'])/100;
	$item['cash_money']=$item['commission_1_1']-$item['service_money'];
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'申请结算',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：只能结算已完成的，待结算订单分佣！'),
			array('type'=>'string','name'=>'commission_1','title'=>'待结算佣金','read'=>true),
			array('type'=>'string','name'=>'commission_1_1','title'=>'可结算佣金','read'=>true),
			array('type'=>'string','name'=>'service_money','title'=>'手续费','read'=>true),
			array('type'=>'string','name'=>'cash_money','title'=>'最终金额','read'=>true),
			array('name'=>'pay_type','title'=>'打款方式','type'=>'radio','options'=>'pay_types'),
			array('type'=>'string','name'=>'wechat','title'=>'微信号','show'=>array('pay_type'=>array('downline'))),
			array('type'=>'string','name'=>'realname','title'=>'真实姓名','show'=>array('pay_type'=>array('alipay','union'))),
			array('type'=>'string','name'=>'alipay','title'=>'支付宝帐号','show'=>array('pay_type'=>array('alipay'))),
			array('type'=>'string','name'=>'bankname','title'=>'所属银行','show'=>array('pay_type'=>array('union'))),
			array('type'=>'string','name'=>'bankcard','title'=>'银行卡号','show'=>array('pay_type'=>array('union'))),
			array('type'=>'string','name'=>'mobile','title'=>'联系电话'),
		),
	);
	if($dhsettimg['withdrawtype']){
		$withdrawtype=array();
		$wxapppay_types=array();
		$check=strstr($dhsettimg['withdrawtype'],'credit');
		if($check){
			$withdrawtype['credit']=$this->pay_types['credit'];
			$wxapppay_types[]=array(
				'key'=>'credit',
				'title'=>$this->pay_types['credit'],
			);
			$pay_type='credit';
		}
		$check=strstr($dhsettimg['withdrawtype'],'wechat');
		if($check){
			$withdrawtype['wechat']=$this->pay_types['wechat'];
			$wxapppay_types[]=array(
				'key'=>'wechat',
				'title'=>$this->pay_types['wechat'],
			);
			$pay_type=$pay_type?:'wechat';
		}
		$check=strstr($dhsettimg['withdrawtype'],'alipay');
		if($check){
			$withdrawtype['alipay']=$this->pay_types['alipay'];
			$wxapppay_types[]=array(
				'key'=>'alipay',
				'title'=>$this->pay_types['alipay'],
			);
			$pay_type=$pay_type?:'alipay';
		}
		$check=strstr($dhsettimg['withdrawtype'],'union');
		if($check){
			$withdrawtype['union']=$this->pay_types['union'];
			$wxapppay_types[]=array(
				'key'=>'union',
				'title'=>$this->pay_types['union'],
			);
			$pay_type=$pay_type?:'union';
		}
		$check=strstr($dhsettimg['withdrawtype'],'downline');
		if($check){
			$withdrawtype['downline']=$this->pay_types['downline'];
			$wxapppay_types[]=array(
				'key'=>'downline',
				'title'=>$this->pay_types['downline'],
			);
			$pay_type=$pay_type?:'downline';
		}
		$return['pay_types']=$withdrawtype;
	}else{
		$return['pay_types']=$this->pay_types;
	}
	exi($return);
}